﻿#include "SettingDialog.h"
#include "ui_SettingDialog.h"
#include "stores/DataStroes.h"
#include "components/Toast/Toast.h"

#include <QProcess>
#include <QDebug>

#define SETTINGS (DataStroes::instance()->settings()->model().data())

SettingDialog::SettingDialog(QWidget *parent)
    : FDialog(parent)
    , ui(new Ui::SettingDialog)
{
    ui->setupUi(this);
    setWindowFlag(Qt::WindowContextHelpButtonHint, false);
    setWindowTitle(tr("Setting"));

    initView();
}

SettingDialog::~SettingDialog()
{
    delete ui;
}

void SettingDialog::initView()
{
    QString lan = SETTINGS.general.language;
    ui->cbLan->setCurrentText(lan);

    // 如果已经全选了则勾选
    ui->cbTlvFilterSelectAll->setChecked(ui->lwTlvTags->isAllChecked());

#if !FULL_VERSION
    // 轻量版本，移除代码编辑器设置页面
    ui->tabWidget->removeTab(3);
#endif
}

void SettingDialog::on_btnCancel_clicked()
{
    reject();
}

void SettingDialog::on_btnSave_clicked()
{
    SETTINGS.general.language = ui->cbLan->currentText();  // general - language

    ui->lwTlvTags->toLogTagFilterModel();  // log - tag_filter

    if (DataStroes::instance()->settings()->save()) {
        Toast::success(tr("Save Successfully!"));
        accept();
    } else {
        Toast::error(tr("Save Failed!"));
    }
}

void SettingDialog::on_cbTlvFilterSelectAll_stateChanged(int arg1)
{
    ui->lwTlvTags->setAllChecked(arg1 == Qt::Checked);
}


void SettingDialog::on_btnTlvFilterApply_clicked()
{
    ui->lwTlvTags->toLogTagFilterModel();
    Toast::success(tr("Tag filter set successfully"));
}


void SettingDialog::on_btnAssociate_clicked()
{
    QProcess process;
    process.start(StoreFilePath::prjFileAssocCmdPath());
    process.waitForFinished();
    Toast::success(tr("Associate Successfully"));
}

